package com.ycsj.goldmedalnewconcept.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.flyco.animation.BaseAnimatorSet;
import com.flyco.dialog.listener.OnBtnLeftClickL;
import com.flyco.dialog.listener.OnBtnRightClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.google.gson.Gson;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.activity.GonggaoDetailActivity.GongaoInfoAdapter;
import com.ycsj.goldmedalnewconcept.bean.AttResponse;
import com.ycsj.goldmedalnewconcept.bean.GonggaoInfo;
import com.ycsj.goldmedalnewconcept.bean.GonggaoResponse;
import com.ycsj.goldmedalnewconcept.bean.HuodongMapResponse;
import com.ycsj.goldmedalnewconcept.bean.HuodongMapResponse.BaomingInfo;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.view.PullToRefreshLayout;
import com.ycsj.goldmedalnewconcept.R;

// 报名名单
public class HuodongMapActivity extends Activity implements OnRefreshListener,OnClickListener {

	private ImageView iv_back;
	private String role;
	private String account;
	private String type;
	private String id;
	private ListView lv_gonggao;
	private GongaoInfoAdapter mAdapter;
	private List<BaomingInfo> Baominglist;
	private List<String> imglist;
	private TextView tv_empty;
	private PullToRefreshLayout refreshLayout;
	private Intent intent;
	private String rowid2;
	
	
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_huodongmap);
		role = SPUtil.getString(this, "role", "");
		account = SPUtil.getString(this, "account", "");

		intent = getIntent();
		rowid2 = intent.getStringExtra("rowid2");
		Log.e("rowid2", rowid2);
		Baominglist = new ArrayList<BaomingInfo>();

		initView();
		setOnListener();
		initData();
	}

	private void initView() {
		iv_back = (ImageView) findViewById(R.id.iv_back);
		lv_gonggao = (ListView) findViewById(R.id.lv_gonggao);
		tv_empty = (TextView) findViewById(R.id.tv_empty);
		refreshLayout = (PullToRefreshLayout)findViewById(R.id.refresh_view);
		refreshLayout.setOnRefreshListener(this);
	}

	private void initData() {

		OkHttpClient mOkHttpClient = new OkHttpClient();
		// mOkHttpClient.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
		FormEncodingBuilder builder = new FormEncodingBuilder();
		builder.add("type", "15");
		builder.add("rowid2", rowid2);
		builder.add("c", account);
		builder.add("role", role);
		Request request = new Request.Builder()
				.url(Constant.SERVER_URL + "PersonalAssistantMsg").post(builder.build())
				.build();
		mOkHttpClient.newCall(request).enqueue(new Callback() {

			// /ycsj_platform/PersonalAssistantMsg?type=15										
			@Override
			public void onFailure(Request arg0, IOException arg1) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						Toast.makeText(getBaseContext(), "网络无响应，请检查网络连接", 0)
								.show();
					}
				});
			}

			@Override
			public void onResponse(Response arg0) throws IOException {
				// 解析Json数据，保存登录状态及身份信息

				Gson gson = new Gson();
				String json = arg0.body().string();
				Log.e("testtt", json);
				
				HuodongMapResponse fromJson = gson.fromJson(json,
						HuodongMapResponse.class);

				if (fromJson != null) {
					if ("-1".equals(fromJson.state)
							|| "0".equals(fromJson.state)) {
						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								tv_empty
								 .setVisibility(tv_empty.VISIBLE);
							}
						});
					} else if ("1".equals(fromJson.state)) {
						Baominglist = fromJson.info;

						mAdapter = new GongaoInfoAdapter();
						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								refreshLayout
								.refreshFinish(PullToRefreshLayout.REFRESH_SUCCEED);
								lv_gonggao.setAdapter(mAdapter);

							}
						});
					}
				}
			}
		});
	}

	private void setOnListener() {
		iv_back.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.iv_back:
			finish();
			break;
		}
	}

	class GongaoInfoAdapter extends BaseAdapter {

		public int getCount() {

			return Baominglist.size();
		}

		public Object getItem(int position) {

			return null;
		}

		public long getItemId(int position) {

			return 0;
		}

		ViewHolder vh = null;

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			if (convertView == null) {
				convertView = View.inflate(parent.getContext(),
						R.layout.item_baoming, null);
				vh = new ViewHolder();

				vh.iv_icon =  (ImageView) convertView.findViewById(R.id.iv_icon);
				vh.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
				vh.tv_classname = (TextView) convertView.findViewById(R.id.tv_classname);
				vh.tv_tel = (TextView) convertView.findViewById(R.id.tv_tel); 
				vh.tv_callphone = (TextView) convertView.findViewById(R.id.tv_callphone); 
				convertView.setTag(vh);
			} else {
				vh = (ViewHolder) convertView.getTag();
			}

			vh.tv_name.setText(Baominglist.get(position).name);
			vh.tv_classname.setText(Baominglist.get(position).classname);
			vh.tv_tel.setText(Baominglist.get(position).tel);
			Picasso.with(HuodongMapActivity.this).load(Baominglist.get(position).icon).into(vh.iv_icon);
			vh.tv_callphone.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					AlertDialog.Builder builder = new AlertDialog.Builder(HuodongMapActivity.this);
					builder.setTitle("联系学生");
				  	builder.setMessage("确定拨打电话吗？");
				  	builder.setPositiveButton("呼叫", new DialogInterface.OnClickListener() {
						
						public void onClick(DialogInterface dialog, int which) {
			                Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+Baominglist.get(position).tel));  
			                startActivity(intent);  
						}
					});
				  	builder.setNegativeButton("取消", null);
				  	builder.show();
				}
			});
			return convertView;
		}

		class ViewHolder {
			private TextView tv_name;
			private TextView tv_classname;
			private TextView tv_tel;
			private TextView tv_callphone;
			private ImageView iv_icon;

		}
	}

	@Override
	public void onRefresh() {
		// 下拉刷新操作
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				initData();
			}
		}, 500);
	}
}







